package us.gaaoc.framework.webapp.backingbeans.agent;
 
import java.util.List;
import java.util.Map;

import javax.faces.component.UIParameter;
import javax.faces.context.FacesContext;
import javax.faces.model.DataModel;
import javax.faces.model.ListDataModel;

import org.apache.log4j.Logger;
import org.hibernate.Hibernate;
import org.springframework.beans.factory.annotation.Configurable;
import org.springframework.transaction.annotation.Transactional;

import us.gaaoc.framework.businessmodel.Filing;
import us.gaaoc.framework.dao.ModelFacade;
import us.gaaoc.framework.dao.ModelFacadeHibernate; 
import us.gaaoc.framework.model.CaseRecord; 
import us.gaaoc.framework.webapp.backingbeans.BaseBackingBean;

/**
 * 
 * @author hea
 * @date Apr 26, 2006
 * @version: GAJE 1.5
 *
 */
public class TrashcanFilingPage extends BaseBackingBean {
 
	/**
	 * Property
	 */
	private DataModel clearedFilingDataTable;
	
	private String courtUuid;
	
 	/**
 	 * Constructor
 	 *
 	 */
	public TrashcanFilingPage(){
		courtUuid = (String) FacesContext.getCurrentInstance().getExternalContext().getSessionMap().get("courtUuid");
		List<Filing> clearedFilings = this.getModelFacade().findClearedCaseFilingsAgentSide(courtUuid);
		clearedFilingDataTable = new ListDataModel();
		clearedFilingDataTable.setWrappedData(clearedFilings);
	}	

	/**
	 * Getter for property clearedFilingDataTable
	 * @return clearedFilingDataTable
	 */
	public DataModel getClearedFilingDataTable() {
		return clearedFilingDataTable;
	}
	
	/**
	 * Setter for property clearedFilingDataTable
	 * @param clearedFilingDataTable
	 */
	public void setClearedFilingDataTable(DataModel clearedFilingDataTable) {
		this.clearedFilingDataTable = clearedFilingDataTable;
	}
  
  /**
   * Rhis method navigates user to the Case Details Page
   * @return String
   */
	public String gotoCaseDetailsPage(){
		Map sessionMap = FacesContext.getCurrentInstance().getExternalContext().getSessionMap();
	 	Map requestMap = FacesContext.getCurrentInstance().getExternalContext().getRequestParameterMap();
	 	String caseUuid = ((String) requestMap.get("caseUuid"));
	 	sessionMap.put("caseUuid", caseUuid);
	 	sessionMap.put("from", "trashcanPage");
	 	return "gotoCaseDetailsPage";
	}
	
	public void setCaseUuid(String caseUuid) {
		Map sessionMap = FacesContext.getCurrentInstance().getExternalContext().getSessionMap();	 	
	 	sessionMap.put("caseUuid", caseUuid);
	 	sessionMap.put("from", "trashcanPage");	 	
	}

	
	
	/**
	 * This method deletes case record from the list. (actually does not delete
	 * from database just changes the delete flag changed)
	 * @return String
	 */
	public String delete(){
	 	Map requestMap = this.getFacesContext().getExternalContext().getRequestParameterMap();
	 	String caseUuid = ((String) requestMap.get("caseUuid"));
	 	ModelFacadeHibernate.getInstance().removeCaseRecord(caseUuid, courtUuid);
	 	//HibernatePhaseListener.getSessionFactory().getCurrentSession().getTransaction().commit();
		return "stay";		
	}
	
	/**
	 * This method restores the case record from the trash can page 
	 * which was cleared
	 * @return String
	 */
	public String restore(){
	 	Map requestMap = this.getFacesContext().getExternalContext().getRequestParameterMap();
	 	String caseUuid = ((String) requestMap.get("caseUuid"));
	 	ModelFacadeHibernate.getInstance().updateCaseRecordForClearCase(caseUuid, false, courtUuid);
	 	//HibernatePhaseListener.getSessionFactory().getCurrentSession().getTransaction().commit();
		return "stay";	
	}
	
	/**
	 * This method navigates user to the previous page
	 * @return String
	 */
	public String back(){		
		return  "allFilingsPage";
	}
	
	/**
	 * This method navigates user to the Case Record History Page
	 * @return String
	 */
	public String gotoHistoryPage() {
		Map sessionMap = this.getFacesContext().getExternalContext()
				.getSessionMap();
		Map requestMap = this.getFacesContext().getExternalContext()
				.getRequestParameterMap();
		String caseUuid = ((String) requestMap.get("caseUuid"));
		sessionMap.put("caseUuid", caseUuid);
		sessionMap.put("from", "trashcanPage");
		return "gotoCaseHistoryPage";
	}
}
